﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DBQLTV;

namespace QuanLyThuVien
{
    public partial class frmPhieuThuTienPhat : Form
    {
        DocGiaBUS docgiaBUS = new DocGiaBUS();
        public frmPhieuThuTienPhat()
        {
            InitializeComponent();

            List<DOCGIA> listDG = docgiaBUS.GetListDocGia();
            foreach (DOCGIA dg in listDG)
            {
                comboBox_maDG.Items.Add(dg.MaDG);
            }
            tb_soTienThu.Text = "0";
        }

        private void UpdateThongTin()
        {
            int maDG = int.Parse(comboBox_maDG.Text);
            if (!comboBox_maDG.Items.Contains(int.Parse(comboBox_maDG.Text)))
            {
                MessageBox.Show("Mã độc giả không tồn tại!");
                tb_soTienThu.Text = "0";
                tb_conLai.Text = "0";
            }
            else
            {
                tb_hoTen.Text = docgiaBUS.GetDocGia(maDG).Ten;
                int tienthu = 0;
                if (tb_soTienThu.Text != "")
                    tienthu = int.Parse(tb_soTienThu.Text);
                tb_conLai.Text = (docgiaBUS.GetDocGia(maDG).TongNo - tienthu).ToString();
                tb_soTienThu.Enabled = true;
            }
        }
        private void button_Huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void button_luu_Click(object sender, EventArgs e)
        {
            if (!comboBox_maDG.Items.Contains(int.Parse(comboBox_maDG.Text)))
                MessageBox.Show("Mã độc giả không tồn tại!");
            else
            {
                docgiaBUS.UpdateTongNo(int.Parse(comboBox_maDG.Text), 0 - int.Parse(tb_soTienThu.Text));
                MessageBox.Show("Thành công!");
                this.Close();
            }
        }

        private void comboBox_maDG_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateThongTin();
        }
        private void comboBox_maDG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                UpdateThongTin();
        }
        private void comboBox_maDG_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void tb_soTienThu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }

        }
        private void tb_soTienThu_KeyUp(object sender, KeyEventArgs e)
        {
            int maDG = int.Parse(comboBox_maDG.Text);
            int tienthu = 0;
            int tongno = docgiaBUS.GetDocGia(maDG).TongNo.Value;
            if (tb_soTienThu.Text != "")
                tienthu = int.Parse(tb_soTienThu.Text);
            if (tienthu > tongno)
            {
                MessageBox.Show("Tiền thu đã lớn hơn tiền nợ");
                tb_soTienThu.Text = tongno.ToString();
            }
            else
            {
                tb_conLai.Text = (tongno - tienthu).ToString();
            }
        }
        private void tb_hoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
